package com.sgb.jicai.viewmodel;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.View;
import androidx.lifecycle.ViewModel;
import com.google.gson.Gson;
import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.entity.CommodityParamValBean;
import com.sgb.jicai.entity.DemanderCompanyPublishBeans;
import com.sgb.jicai.entity.FilterEntity;
import com.sgb.jicai.entity.JiCaiPublishListBean;
import com.sgb.link.entity.PublicTypeEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.jicai.view.activity.JiCaiDemanderPublishAct;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Time:2021/8/9
 * AUTHOR:F_Q
 * Description:
 */
public class DemanderPublishModel extends ViewModel {

    JiCaiDemanderPublishAct activity;
    /**
     * 获取用户列表数据
     */
    public void getUserList(View view, boolean isboole) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getUserList().enqueue(new ICallBack<BaseEntity<List<JiCaiPublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<JiCaiPublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.setUserList(data.getData(), view, isboole);
                }
            }
        });

    }

    /**
     * 公司列表信息
     */
    public void getCompanyList(View view, boolean isShow) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getMyCompanyList().enqueue(new ICallBack<BaseEntity<List<JiCaiPublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<JiCaiPublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setCompanyList(data.getData(), view, isShow);
                    } else {
                        MToast.showToast(activity, "当前用户没有其它公司信息");
                    }
                }
            }
        });
    }


    /**
     * 部门列表信息
     */
    public void getDepartmentList(String companyNo, View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getDeptList(companyNo).enqueue(new ICallBack<BaseEntity<List<JiCaiPublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<JiCaiPublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setDepartmentList(data.getData(), view);
                    } else {
                        MToast.showToast(activity, "当前单位无部门信息");
                    }
                }
            }
        });
    }

    /**
     * 获取地址信息
     */
    public void getAddressList(View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.initAddressData(data.getData(), view);
                }
            }
        });
    }


    /**
     * 获取付款方式
     */
    public void getPaymentType(View view) {
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("预付款", "", "1"));
        bean.add(new JiCaiPublishListBean("货到付款", "", "2"));
        bean.add(new JiCaiPublishListBean("其他", "", "3"));
        activity.setPaymentList(bean, view);
    }

    /**
     * 发票类型
     */
    public void getInvoiceType(View view) {
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("不带票", "", "1"));
        bean.add(new JiCaiPublishListBean("增值税专用票", "", "2"));
        bean.add(new JiCaiPublishListBean("增值税普通发票", "", "3"));
        activity.setInvoiceList(bean, view);
    }

    /**
     * 税率
     */
    public void getTaxRate(View view) {
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("3%", "", ""));
        bean.add(new JiCaiPublishListBean("6%", "", ""));
        bean.add(new JiCaiPublishListBean("9%", "", ""));
        bean.add(new JiCaiPublishListBean("11%", "", ""));
        bean.add(new JiCaiPublishListBean("13%", "", ""));
        activity.setTaxRateList(bean, view);
    }

    /**
     * 单位
     */
    public void getUnitList(View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getfindUnitList().enqueue(new ICallBack<BaseEntity<List<JiCaiPublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<JiCaiPublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setUnitList(data.getData(), view);
                    } else {
                        MToast.showToast(activity, "没有单位信息");
                    }
                }
            }
        });
    }

    /**
     * 获取商品列表
     */
    public void getGoodsCategory() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getGoodsCategory("").enqueue(new ICallBack<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.showGoodsTypePop(data.getData());
                }
            }
        });
    }

    /**
     * 质保金单位
     * @param view
     */
    public void getDepositRateUnit(View view) {
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("元", "1", "请输入具体金额"));
        bean.add(new JiCaiPublishListBean("%", "2", "请输入质保金百分比"));
        activity.setDepositRateUnit(bean, view);
    }

    /**
     * 质保周期单位
     */
    public void getTimeUnit(View view) {
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("日", "", ""));
        bean.add(new JiCaiPublishListBean("月", "", ""));
        bean.add(new JiCaiPublishListBean("年", "", ""));
        activity.setTimeUnit(bean, view);
    }

    /**
     * 集采类型
     *
     * @param view
     */
    public void getJiCaiTypeList(View view) {
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("商品材料", "1", ""));
        bean.add(new JiCaiPublishListBean("机械租赁", "2", ""));
        bean.add(new JiCaiPublishListBean("设备租赁", "3", ""));
        activity.setJiCaiTypeList(bean, view);
    }

    /**
     * 供方要求
     *
     * @param view
     */
    public void getSupplierRequireList(View view) {
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("生产供方", "", ""));
        bean.add(new JiCaiPublishListBean("一级经销商", "", ""));
        bean.add(new JiCaiPublishListBean("二级经销商", "", ""));
        bean.add(new JiCaiPublishListBean("三级经销商", "", ""));
        bean.add(new JiCaiPublishListBean("不限", "", ""));
        activity.setSupplierRequireList(bean, view);
    }

    /**
     * 注册资金
     *
     * @param view
     */
    public void getRegisteredFundList(View view) {
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("100万元以上", "", ""));
        bean.add(new JiCaiPublishListBean("500万元以上", "", ""));
        bean.add(new JiCaiPublishListBean("1000万元以上", "", ""));
        bean.add(new JiCaiPublishListBean("2000万元以上", "", ""));
        bean.add(new JiCaiPublishListBean("不限", "", ""));
        activity.setRegisteredFundList(bean, view);
    }

    /**
     * 经营业绩
     *
     * @param view
     */
    public void getOperationResultList(View view) {
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("年营业额100万元以上", "", ""));
        bean.add(new JiCaiPublishListBean("年营业额500万元以上", "", ""));
        bean.add(new JiCaiPublishListBean("年营业额1000万元以上", "", ""));
        bean.add(new JiCaiPublishListBean("年营业额2000万元以上", "", ""));
        bean.add(new JiCaiPublishListBean("不限", "", ""));
        activity.setOperationResultList(bean, view);
    }

    /**
     * 存货要求
     *
     * @param view
     */
    public void getInventoryRequirementsList(View view) {
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("库存量达到采购量一倍以上", "", ""));
        bean.add(new JiCaiPublishListBean("库存量达到采购量接近", "", ""));
        bean.add(new JiCaiPublishListBean("库存不限", "", ""));
        activity.setInventoryRequirementsList(bean, view);
    }

    /**
     * 专业要求
     *
     * @param view
     */
    public void getProfessionRequireList(View view) {
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("经营同类商品证明", "", ""));
        bean.add(new JiCaiPublishListBean("专业不限", "", ""));
        activity.setProfessionRequireList(bean, view);
    }

    /**
     * 商品sku属性
     */
    public void getSku(String cid3) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getCommodityParamVal(cid3).enqueue(new ICallBack<BaseEntity<List<CommodityParamValBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommodityParamValBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals("ok")) {
                    activity.setSkuList(data.getData());
                }

            }
        });
    }

    /**
     * 机械租赁列表
     */
    public void getMachineTypeModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getMachineOutType().enqueue(new ICallBack<BaseEntity<List<PublicTypeEntity<List<PublicTypeEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublicTypeEntity<List<PublicTypeEntity>>>> data) {
                DialogHelper.dismissProgressDialog();
                activity.getMachineTypeSucc(data.getData());
            }
        });
    }

    /**
     * 设备列表
     */
    @SuppressLint("CheckResult")
    public void getParentDeviceType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getDeviceType("0").enqueue(new ICallBack<BaseEntity<List<PublicTypeEntity<List<PublicTypeEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublicTypeEntity<List<PublicTypeEntity>>>> data) {
                DialogHelper.dismissProgressDialog();
                activity.showDeviceType(data.getData());
            }
        });
    }

    /**
     * 设备和机械属性接口
     *
     * @param categoryType
     * @param firstCategoryNo
     * @param secondCategoryNo
     */
    public void getDeviceAttr(String categoryType, String firstCategoryNo, String secondCategoryNo) {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        switch (categoryType) {
            case "1"://商品材料
                break;
            case "2"://机械租赁
                map.put("categoryType", "1");//1为机械、2为设备
                break;
            case "3"://设备租赁
                map.put("categoryType", "2");//1为机械、2为设备
                break;
        }
        map.put("firstCategoryNo", firstCategoryNo);
        map.put("secondCategoryNo", secondCategoryNo);
        JiCaiBaseApi.getApi().getAttributeList(map).enqueue(new ICallBack<BaseEntity<List<FilterEntity<List<FilterEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<FilterEntity<List<FilterEntity>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData()== null || data.getState() == null) return;
                activity.setAttr(data.getData());
            }
        });
    }

    /**
     * 服务费收取方式
     *
     * @param view
     */
    public void getChargeWayList(View view) {
        /**
         * 服务收费标准 1.按照成交额比例收取 2按照每单固定收费 3供方承担 4需方承担
         * */
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("按照成交额比例收取", "1", ""));
        bean.add(new JiCaiPublishListBean("按照每单固定收费", "2", ""));
        bean.add(new JiCaiPublishListBean("供方承担", "3", ""));
        bean.add(new JiCaiPublishListBean("需方承担", "4", ""));
        activity.setChargeWayList(bean, view);
    }

    /**
     * 送货方式
     *
     * @param view
     */
    public void getShippingMethodList(View view) {
        /**
         * 送货方式 1自提 2 供方配送 3发起方配送
         */
        List<JiCaiPublishListBean> bean = new ArrayList<>();
        bean.add(new JiCaiPublishListBean("需方自提", "1", ""));
        bean.add(new JiCaiPublishListBean("供方配送", "2", ""));
        bean.add(new JiCaiPublishListBean("发送方配送", "3", ""));
        activity.setShippingMethodList(bean, view);
    }

    /**
     * 发布
     *
     * @param bean
     */
    public void setJiCaiDemanderPublish(DemanderCompanyPublishBeans bean) {
        Log.e("发布接口数据", new Gson().toJson(bean));
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getAddGroupBuying(bean).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    MToast.showToast(activity, "发布成功");
                    activity.SucceedView();
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    public void setActivity(JiCaiDemanderPublishAct jiCaiDemanderPublishAct) {
        this.activity = jiCaiDemanderPublishAct;
    }
}
